//
//  YZSportTracking.m
//  runrun
//
//  Created by yongzhen on 16/10/21.
//  Copyright © 2016年 yongzhen. All rights reserved.
//

#import "YZSportTracking.h"

@implementation YZSportTracking
{
    CLLocation *_startLocation;
    NSMutableArray <YZSportTrackingLine *> *_trackinglines;
}
- (instancetype)initWithType:(YZSportType)type sportStatus:(YZSportStatus)sportstatus
{
    self = [super init];
    if (self) {
        
        _sportType = type;
        _trackinglines = [NSMutableArray new];
        _sportStatus = sportstatus;
    }
    return self;
}
-(void)setSportStatus:(YZSportStatus)sportStatus
{
    _sportStatus = sportStatus;
    if (_sportStatus != YZSportStatusContinue) {
        _startLocation = nil;
    }
}
-(CLLocation *)sportStartLocation
{
    return _trackinglines.firstObject.startLocation;
    
}
-(UIImage *)sportImage
{
    UIImage *image;
    switch (_sportType) {
        case YZSportTypeRun:
            image = [UIImage imageNamed:@"map_annotation_run"];
            break;
        case YZSportTypeWalk:
            image = [UIImage imageNamed:@"map_annotation_walk"];
            break;
            
        case YZSportTypeRide:
            image = [UIImage imageNamed:@"map_annotation_bike"];
         break;
            
        default:
            break;
    
    }
    return image;
    
}
-(YZSportGPSStatus)gpsSinal
{
    YZSportGPSStatus state = YZSportGPSStatusDisconnect;
    
    return state;
    
}

-(YZSportPloyline *)appendLocation:(CLLocation *)location
{
//    NSLog(@"%f",[[NSDate date] timeIntervalSinceDate:location.timestamp]);
    
    
    if (
        [self gpsSinal] < YZSportGPSStatusNormal) {
        return nil;
    }
    
    
    // 判断速度
    if (location.speed <= 0) {
        return nil;
    }
    
    CGFloat factor = 2;
    if ([[NSDate date] timeIntervalSinceDate:location.timestamp] > factor) {
        return nil;
    }
    
    
    
    if (_startLocation == nil) {
        _startLocation = location;
        return nil;
    }
    
    
    
    YZSportTrackingLine *trankLine = [[YZSportTrackingLine alloc] initWithStartLocation:_startLocation endLocation:location];
    
    [_trackinglines addObject:trankLine];
    
    // kvc
//    NSLog(@"----%@",[_trackinglines valueForKeyPath:@"@sum.time"]);
    NSLog(@"速度%f---时间%f--距离%f---最大速度%f",self.avgSpeed,self.totalTime,self.totalDistance,self.maxSpeed);
    
    _startLocation = location;
    
    return trankLine.polyline;
    
}
-(double)avgSpeed
{
    return [[_trackinglines valueForKeyPath:@"@avg.speed"] doubleValue];
    
}

-(double)maxSpeed
{
    return [[_trackinglines valueForKeyPath:@"@max.speed"] doubleValue];
    
}
-(double)totalTime
{
    return [[_trackinglines valueForKeyPath:@"@sum.time"] doubleValue];
    
}

-(double)totalDistance
{
    return [[_trackinglines valueForKeyPath:@"@sum.distance"] doubleValue];
    
}
- (NSString *)totalTimeStr {
    
    NSInteger totalTime = (NSInteger)self.totalTime;
    return [NSString stringWithFormat:@"%02zd:%02zd:%02zd", totalTime / 3600, (totalTime % 3600) / 60, totalTime % 60];
}












@end
